#include "qborderwidget.h"
#include "ui_qborderwidget.h"

#include "../qimagechoosedialog.h"
#include "../qsoftcore.h"

#include "../../sharedLib/qresourcemanager.h"
#include "../../sharedLib/qprojectresource.h"

#include <QPixmap>
#include <QColorDialog>

using namespace CORELIB;

QBorderWidget::QBorderWidget(QWidget *parent) :
    QAbstractStylesheetWidget(parent),
    ui(new Ui::QBorderWidget)
{
    ui->setupUi(this);
    this->setProperty("no-ManhattanStyle",true);
    this->setFixedHeight(this->height());

    init();
}

QBorderWidget::~QBorderWidget()
{
    delete ui;
    if(m_tempResource!="")
    {
        SHAREDLIB::QProjectResource::resource()->removeResource(m_tempResource);
    }
}

void QBorderWidget::setItem(const tagStylesheetItem &item)
{
    disconnect(ui->left_style,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_left_style_currentIndexChanged(QString)));
    disconnect(ui->top_style,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_top_style_currentIndexChanged(QString)));
    disconnect(ui->right_style,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_right_style_currentIndexChanged(QString)));
    disconnect(ui->bottom_style,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_bottom_style_currentIndexChanged(QString)));

    disconnect(ui->left_width,SIGNAL(valueChanged(QString)),this,SLOT(on_left_width_valueChanged(QString)));
    disconnect(ui->top_width,SIGNAL(valueChanged(QString)),this,SLOT(on_top_width_valueChanged(QString)));
    disconnect(ui->bottom_width,SIGNAL(valueChanged(QString)),this,SLOT(on_bottom_width_valueChanged(QString)));
    disconnect(ui->right_width,SIGNAL(valueChanged(QString)),this,SLOT(on_right_width_valueChanged(QString)));
    QGradient g;

    g=item.m_attributes.value("left_color").value<QGradient>();
    setColor(ui->left_color,"left_color",g);

    g=item.m_attributes.value("right_color").value<QGradient>();
    setColor(ui->right_color,"right_color",g);

    g=item.m_attributes.value("top_color").value<QGradient>();
    setColor(ui->top_color,"top_color",g);

    g=item.m_attributes.value("bottom_color").value<QGradient>();
    setColor(ui->bottom_color,"bottom_color",g);

    ui->left_width->setValue(item.m_attributes.value("left_width").toInt());
    ui->top_width->setValue(item.m_attributes.value("top_width").toInt());
    ui->bottom_width->setValue(item.m_attributes.value("bottom_width").toInt());
    ui->right_width->setValue(item.m_attributes.value("right_width").toInt());

    ui->same_color->setChecked(item.m_attributes.value("color_same","true")=="true");
    ui->same_style->setChecked(item.m_attributes.value("style_same","true")=="true");
    ui->same_width->setChecked(item.m_attributes.value("width_same","true")=="true");

    on_same_width_clicked();
    on_same_color_clicked();
    on_same_style_clicked();

    QString str;
    str=item.m_attributes.value("left_style").toString();
    ui->left_style->setCurrentIndex(ui->left_style->findText(str));
    str=item.m_attributes.value("top_style").toString();
    ui->top_style->setCurrentIndex(ui->top_style->findText(str));
    str=item.m_attributes.value("right_style").toString();
    ui->right_style->setCurrentIndex(ui->right_style->findText(str));
    str=item.m_attributes.value("bottom_style").toString();
    ui->bottom_style->setCurrentIndex(ui->bottom_style->findText(str));

    QAbstractStylesheetWidget::setItem(item);

    connect(ui->left_style,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_left_style_currentIndexChanged(QString)));
    connect(ui->top_style,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_top_style_currentIndexChanged(QString)));
    connect(ui->right_style,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_right_style_currentIndexChanged(QString)));
    connect(ui->bottom_style,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_bottom_style_currentIndexChanged(QString)));

    connect(ui->left_width,SIGNAL(valueChanged(QString)),this,SLOT(on_left_width_valueChanged(QString)));
    connect(ui->top_width,SIGNAL(valueChanged(QString)),this,SLOT(on_top_width_valueChanged(QString)));
    connect(ui->bottom_width,SIGNAL(valueChanged(QString)),this,SLOT(on_bottom_width_valueChanged(QString)));
    connect(ui->right_width,SIGNAL(valueChanged(QString)),this,SLOT(on_right_width_valueChanged(QString)));
}

void QBorderWidget::takeResource()
{
    m_tempResource="";
}

void CORELIB::QBorderWidget::on_image_clicked()
{
    QImageChooseDialog dlg(this);
    dlg.exec();
    QString first,last;
    dlg.getImage(first,last);

    if(first!="" && last!="")
    {
        if(last!=m_item.m_attributes.value("image").toString())
        {
            QByteArray data=QSoftCore::getCore()->resourceManager()->resourceData(first,last);
            if(data.size()>0)
            {
                SHAREDLIB::QProjectResource::resource()->addResource(last,data);
                m_item.m_attributes.insert("image",last);
                if(m_tempResource!="")
                {
                    SHAREDLIB::QProjectResource::resource()->removeResource(m_tempResource);
                }
                m_tempResource=last;
                makeStylesheet();
            }
        }
    }
}

void QBorderWidget::init()
{
    QStringList list;

    list<<"dashed"<<"dot-dash"<<"dot-dot-dash"<<"dotted"<<"double"
       <<"solid"<<"none";
    ui->left_style->addItems(list);
    ui->top_style->addItems(list);
    ui->right_style->addItems(list);
    ui->bottom_style->addItems(list);

    ui->left_style->setCurrentIndex(5);
    ui->top_style->setCurrentIndex(5);
    ui->right_style->setCurrentIndex(5);
    ui->bottom_style->setCurrentIndex(5);

    QGradient g=QLinearGradient(0,0,1,0);
    g.setColorAt(0,QColor(0,0,0,255));
    g.setColorAt(1,QColor(0,0,0,255));

    setColor(ui->left_color,"left_color",g);
    setColor(ui->top_color,"top_color",g);
    setColor(ui->right_color,"right_color",g);
    setColor(ui->bottom_color,"bottom_color",g);
}

QIcon QBorderWidget::createIcon(const QGradient &gradient)
{
    QPixmap pixmap=QPixmap(16,16);
    pixmap.fill(gradient.stops().first().second);
    QIcon icon=QIcon(pixmap);
    icon.addPixmap(pixmap,QIcon::Disabled);
    return icon;
}

void CORELIB::QBorderWidget::on_left_color_clicked()
{
    QGradient g=getColor();
    if(g.type()!=QGradient::NoGradient)
    {
        setColor(ui->left_color,"left_color",g);
        if(ui->same_color->isChecked())
        {
            setColor(ui->top_color,"top_color",g);
            setColor(ui->right_color,"right_color",g);
            setColor(ui->bottom_color,"bottom_color",g);
        }
    }
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_top_color_clicked()
{
    QGradient g=getColor();
    if(g.type()!=QGradient::NoGradient)
    {
        setColor(ui->top_color,"top_color",g);
    }
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_right_color_clicked()
{
    QGradient g=getColor();
    if(g.type()!=QGradient::NoGradient)
    {
        setColor(ui->right_color,"right_color",g);
    }
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_bottom_color_clicked()
{
    QGradient g=getColor();
    if(g.type()!=QGradient::NoGradient)
    {
        setColor(ui->bottom_color,"bottom_color",g);
    }
    makeStylesheet();
}

void QBorderWidget::setColor(QPushButton *btn, const QString &name, const QGradient &color)
{
    QGradient g=color;
    if(g.type()==QGradient::NoGradient)
    {
        g.setColorAt(0,QColor(0,0,0,0));
    }
    btn->setIcon(createIcon(g));
    QString str;
    QColor c=g.stops().first().second;
    str=str.sprintf("#%02X%02X%02X%02X",c.alpha(),c.red(),c.green(),c.blue());
    btn->setText(str);
    QVariant v;
    v.setValue<QGradient>(g);
    m_item.m_attributes.insert(name,v);
}

QGradient QBorderWidget::getColor()
{
    QColor c=QColorDialog::getColor(Qt::white,this);
    if(c.isValid())
    {
        QGradient g=QLinearGradient(0,0,1,0);
        g.setColorAt(0,c);
        g.setColorAt(1,c);
        return g;
    }
    else
    {
        return QGradient();
    }
}

void CORELIB::QBorderWidget::on_same_style_clicked()
{
    ui->top_style->setEnabled(!ui->same_style->isChecked());
    ui->right_style->setEnabled(!ui->same_style->isChecked());
    ui->bottom_style->setEnabled(!ui->same_style->isChecked());
    if(ui->same_style->isChecked())
    {
        QString str=ui->left_style->currentText();
        ui->top_style->setCurrentIndex(ui->top_style->findText(str));
        ui->right_style->setCurrentIndex(ui->top_style->findText(str));
        ui->bottom_style->setCurrentIndex(ui->top_style->findText(str));
    }
    m_item.m_attributes.insert("style_same",ui->same_style->isChecked()?"true":"false");
}

void CORELIB::QBorderWidget::on_same_width_clicked()
{
    ui->top_width->setEnabled(!ui->same_width->isChecked());
    ui->right_width->setEnabled(!ui->same_width->isChecked());
    ui->bottom_width->setEnabled(!ui->same_width->isChecked());
    if(ui->same_width->isChecked())
    {
        int v=ui->left_width->value();
        ui->top_width->setValue(v);
        ui->right_width->setValue(v);
        ui->bottom_width->setValue(v);
    }
    m_item.m_attributes.insert("width_same",ui->same_width->isChecked()?"true":"false");
}

void CORELIB::QBorderWidget::on_same_color_clicked()
{
    ui->top_color->setEnabled(!ui->same_color->isChecked());
    ui->right_color->setEnabled(!ui->same_color->isChecked());
    ui->bottom_color->setEnabled(!ui->same_color->isChecked());
    if(ui->same_color->isChecked())
    {
        QGradient g=m_item.m_attributes.value("left_color").value<QGradient>();
        setColor(ui->top_color,"top_color",g);
        setColor(ui->right_color,"right_color",g);
        setColor(ui->bottom_color,"bottom_color",g);
        makeStylesheet();
    }
    m_item.m_attributes.insert("color_same",ui->same_color->isChecked()?"true":"false");
}

void CORELIB::QBorderWidget::on_left_style_currentIndexChanged(const QString &arg1)
{
    QString str=arg1;
    m_item.m_attributes.insert("left_style",str);
    if(ui->same_style->isChecked())
    {
        ui->top_style->setCurrentIndex(ui->top_style->findText(arg1));
        ui->right_style->setCurrentIndex(ui->right_style->findText(arg1));
        ui->bottom_style->setCurrentIndex(ui->bottom_style->findText(arg1));
    }
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_top_style_currentIndexChanged(const QString &arg1)
{
    QString str=arg1;
    m_item.m_attributes.insert("top_style",str);
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_right_style_currentIndexChanged(const QString &arg1)
{
    QString str=arg1;
    m_item.m_attributes.insert("right_style",str);
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_bottom_style_currentIndexChanged(const QString &arg1)
{
    QString str=arg1;
    m_item.m_attributes.insert("bottom_style",str);
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_left_width_valueChanged(const QString &arg1)
{

    QString str=arg1;
    m_item.m_attributes.insert("left_width",str);
    if(ui->same_width->isChecked())
    {
        int v=ui->left_width->value();
        ui->top_width->setValue(v);
        ui->right_width->setValue(v);
        ui->bottom_width->setValue(v);
    }
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_top_width_valueChanged(const QString &arg1)
{
    QString str=arg1;
    m_item.m_attributes.insert("top_width",str);
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_right_width_valueChanged(const QString &arg1)
{
    QString str=arg1;
    m_item.m_attributes.insert("right_width",str);
    makeStylesheet();
}

void CORELIB::QBorderWidget::on_bottom_width_valueChanged(const QString &arg1)
{
    QString str=arg1;
    m_item.m_attributes.insert("bottom_width",str);
    makeStylesheet();
}
